package com.theaf.core.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.theaf.core.api.DicComService;
import com.theaf.core.api.model.DicData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
    * 字典的帮助类。
    * 为了加快速度同一个字典只会加载一次
    * @author songwenke
    * @date 2017-11-15
    *
    */
public class DictionaryHelper {
	private static final int MAP_INIT_SIZE=10;

	protected static Logger logger= LoggerFactory.getLogger(DictionaryHelper.class);

	private static DicComService dictComService  ;

	private static void init(){
		if(dictComService==null) {
			dictComService=SpringUtils.getDubboService(DicComService.class);
			if(dictComService==null) {
				SmExceptionHelper.sysUnknownException("找不到字典服务");
			}
		}
	}

	
	/*  *
	    * 效果等于 EnumService.queryList
	    * @param path
	    * @return 
	    * Collection    返回类型
	    * @throws
	    */
	public static Collection<DicData> getResult(String path)
	{
		init();
		return dictComService.findDicDataByTypeTag(path);
	}
	
	/**
	 * 返回一个Map
	 * name 为 key,desc 为value
	 * @param path
	 * @return
	 * Map
	 */
	public static Map getResultMap(String path)
	{
		Collection coll = getResult(path);
		if( coll == null ) {
			return null;
		}
		Map m = new HashMap(MAP_INIT_SIZE);
		Iterator iter = coll.iterator();
		while( iter.hasNext() )
		{
			DicData o = (DicData)iter.next();
			m.put(o.getCode(), o.getName());
		}
		return m;
	}

	
	/**
	 * 返回一个Map
	 * memo 为value
	 * @param path
	 * @return
	 * Map
	 */
	public static Map getResultMapMemo(String path)
	{
		Collection coll = getResult(path);
		if( coll == null ) {
			return null;
		}
		Map m = new HashMap(MAP_INIT_SIZE);
		Iterator iter = coll.iterator();
		while( iter.hasNext() )
		{
			DicData o = (DicData)iter.next();
			m.put(o.getCode(), o.getRemark());
		}
		return m;
	}
	
	
   /**
	*
	* 直接访问子项
	* @param path
	* @param chlidKey
	* @return
	* String    返回类型
	* @throws
	*/
	public static String getChlidValue(String path,String chlidKey)
	{
		String v = null;
		Collection coll  =  getResult(path);
		if( coll == null ) {
			return null;
		}
		Iterator iter = coll.iterator();
		while(iter.hasNext())
		{
			DicData o = (DicData)iter.next();
			if( chlidKey.equals( o.getCode() ) )
			{
				v = o.getName();
				break;
			}
		}
		return v;
	}
		/**
		 * 从多个父级中查询字典。
		 * 数组的最后一个为子级的key
	 	 * @param path
	 	 * @return
	 	 * String
	 	 */
	public static String getChlidValue(String ...path)
	{
		String chlidKey = path[path.length - 1];
		String v = null;
		for( int i=0;i<path.length-1;i++)
		{
			v = getChlidValue(path[i], chlidKey);
			if( StringUtils.isNotEmpty(v) )
			{
				break;
			}
		}		
		return v;		
	}
		/**
		 * 从多个父级中查询字典备注。
		 * 数组的最后一个为子级的key
		 * 
	 	 * @param path
	 	 * @return
	 	 * String
	 	 */
	public static String getChlidMemo(String ...path)
	{
		String chlidKey = path[path.length - 1];
		String v = null;
		for( int i=0;i<path.length-1;i++)
		{
			v = getChlidMemo(path[i], chlidKey);
			if( StringUtils.isNotEmpty(v) )
			{
				break;
			}
		}		
		return v;		
	}

	/**
	 * 查询字典数据的备注信息
	 * @param path
	 * @param chlidKey
	 * @return
	 */
	public static String getChlidMemo(String path,String chlidKey)
	{
		String v = null;
		Collection coll  =  getResult(path);
		Iterator iter = coll.iterator();
		try
		{
			while(iter.hasNext())
			{
				DicData o = (DicData)iter.next();
				if( chlidKey.equals( o.getCode() ) )
				{
					v = o.getRemark();
					break;
				}
			}
		}catch (Exception e) {
		}
		return v;
	}
}
